package top.deepdesigner.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import top.deepdesigner.dto.ApproveAgencyDTO;
import top.deepdesigner.mapper.DtsUserAccountMapper;
import top.deepdesigner.mapper.DtsUserMapper;
import top.deepdesigner.pojo.DtsUser;
import top.deepdesigner.pojo.DtsUserAccount;
import top.deepdesigner.service.DtsUserService;
import top.deepdesigner.vo.DayStatis;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * Start up <br/>
 * Created with IntelliJ IDEA. <br/>
 * Project Name: shop_manage <br/>
 * Author: duanruiqing <br/>
 * DateTime: 2022/9/20 星期二 20:22:32 <br/>
 */
@Service
public class DtsUserServiceImpl extends ServiceImpl<BaseMapper<DtsUser>, DtsUser> implements DtsUserService {
  @Resource
  private DtsUserMapper dtsUserMapper;

  @Resource
  private DtsUserAccountMapper userAccountMapper;

  @Override
  public Long getUserCount() {
    return this.count(null);
  }

  @Override
  public List<DayStatis> getUserTotalAndCountByDate(Integer days) {
    return dtsUserMapper.selectOrderTotalAndCountByDate(days);
  }

  @Override
  public List<DtsUser> getUserList(Integer page, Integer limit, String sort, String order, String username, String mobile) {
    QueryWrapper<DtsUser> wrapper = new QueryWrapper<>();
    wrapper
        .like(!StringUtils.isEmpty(username), "username", username)
        .like(!StringUtils.isEmpty(mobile), "mobile", mobile)
        .orderBy(true, "asc".equalsIgnoreCase(order), sort);
    Page<DtsUser> userPage = new Page<>(page, limit);
    return this.page(userPage, wrapper).getRecords();
  }

  @Override
  public DtsUser getUserById(Integer userId) {
    return this.getOne(new LambdaQueryWrapper<DtsUser>()
        .eq(!StringUtils.isEmpty(userId), DtsUser::getId, userId)
        .eq(true, DtsUser::getDeleted, 0));
  }

  @Override
  public List<DtsUser> getUserByName(String username) {
    return this.list(new LambdaQueryWrapper<DtsUser>().like(!StringUtils.isEmpty(username), DtsUser::getUsername, username));
  }

  @Override
  public boolean approveAgency(ApproveAgencyDTO approveAgencyDTO) {
    Integer userId = approveAgencyDTO.getUserId();
    Integer settlementRate = approveAgencyDTO.getSettlementRate();

    //新增userAccount数据
    DtsUserAccount dtsUserAccount = new DtsUserAccount();
    dtsUserAccount.setSettlementRate(settlementRate);
    dtsUserAccount.setRemainAmount(new BigDecimal(0));
    dtsUserAccount.setTotalAmount(new BigDecimal(0));
    dtsUserAccount.setCreateTime(new Date());
    dtsUserAccount.setModifyTime(new Date());
    dtsUserAccount.setUserId(userId);
    dtsUserAccount.setStatus(1);
    userAccountMapper.insert(dtsUserAccount);

    //更新user表权限
    DtsUser dtsUser = new DtsUser();
    dtsUser.setUpdateTime(new Date());
    dtsUser.setUserLevel(2);
    dtsUser.setStatus(0);
    dtsUserMapper.update(dtsUser, new LambdaUpdateWrapper<DtsUser>()
        .eq(DtsUser::getId, userId));
    return true;
  }
}
